package com.ruoyi.jiayou.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jiayou.api.RemoteCfCarService;
import com.ruoyi.jiayou.api.RemoteCfUserService;
import com.ruoyi.jiayou.api.domain.CfCar;
import com.ruoyi.jiayou.api.domain.CfUser;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.jiayou.domain.CfRescue;
import com.ruoyi.jiayou.service.ICfRescueService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救援任务Controller
 * 
 * @author zhangbiao
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/rescue")
public class CfRescueController extends BaseController
{
    @Autowired
    private ICfRescueService cfRescueService;
    @Autowired
    private RemoteCfUserService remoteCfUserService;
    @Autowired
    private RemoteCfCarService remoteCfCarService;

    /**
     * 查询救援任务列表
     */
//    @RequiresPermissions("jiayou:rescue:list")
    @GetMapping("/list")
    public TableDataInfo list(CfRescue cfRescue)
    {
        startPage();
        List<CfRescue> list = cfRescueService.selectCfRescueList(cfRescue);
        for (CfRescue rescue : list){
            AjaxResult info = remoteCfCarService.getInfo(rescue.getCarId());
            Map<String,Object> dataMap = (Map<String, Object>) info.get("data");
            rescue.setCarNumber((String) dataMap.get("carNumber"));
            AjaxResult info1 = remoteCfUserService.getInfo(rescue.getUserId());
            Map<String,Object> dataMap2 = (Map<String, Object>) info1.get("data");
            rescue.setUserName((String) dataMap2.get("realName"));
            rescue.setUserPhone((String) dataMap2.get("phone"));
        }
        return getDataTable(list);
    }

    /**
     * 导出救援任务列表
     */
    @RequiresPermissions("jiayou:rescue:export")
    @Log(title = "救援任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CfRescue cfRescue)
    {
        List<CfRescue> list = cfRescueService.selectCfRescueList(cfRescue);
        ExcelUtil<CfRescue> util = new ExcelUtil<CfRescue>(CfRescue.class);
        util.exportExcel(response, list, "救援任务数据");
    }

    /**
     * 获取救援任务详细信息
     */
    @RequiresPermissions("jiayou:rescue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cfRescueService.selectCfRescueById(id));
    }

    /**
     * 新增救援任务
     */
    @RequiresPermissions("jiayou:rescue:add")
    @Log(title = "救援任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CfRescue cfRescue)
    {
        return toAjax(cfRescueService.insertCfRescue(cfRescue));
    }

    /**
     * 修改救援任务
     */
    @RequiresPermissions("jiayou:rescue:edit")
    @Log(title = "救援任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CfRescue cfRescue)
    {
        return toAjax(cfRescueService.updateCfRescue(cfRescue));
    }

    /**
     * 删除救援任务
     */
    @RequiresPermissions("jiayou:rescue:remove")
    @Log(title = "救援任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cfRescueService.deleteCfRescueByIds(ids));
    }
}
